﻿using System;
using System.Text;
using Reactor.ServiceGrid;

namespace Reactor.Messaging
{
    /// <summary>
    /// MessagingChannel implementation representing a queue.
    /// </summary>
    [Serializable]
    public class PointToPointChannel : MessagingChannel
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="PointToPointChannel"/> class.
        /// </summary>
        public PointToPointChannel() {}

        /// <summary>
        /// Initializes a new instance of the <see cref="PointToPointChannel"/> class.
        /// </summary>
        /// <param name="name">The name.</param>
        public PointToPointChannel(string name)
        {
            DestinationName = name;
        }

        #endregion

        /// <summary>
        /// Performs an implicit conversion from <see cref="System.String"/> to <see cref="PointToPointChannel"/>.
        /// </summary>
        /// <param name="stringValue">The string value.</param>
        /// <returns>The result of the conversion.</returns>
        public static implicit operator PointToPointChannel(string stringValue)
        {
            return new PointToPointChannel(stringValue);
        }

        /// <summary>
        /// Creates the name for a managed service's primary input channel.
        /// </summary>
        /// <param name="serviceIdentifier">The service identifier.</param>
        /// <returns></returns>
        public static string CreateNameForManagedServiceInputChannel(ServiceIdentifier serviceIdentifier)
        {
            return string.Format("Reactor.Service.{0}-{1}", serviceIdentifier.Name, serviceIdentifier.Version);
        }

        /// <summary>
        /// Creates the name for a Reactor Core's primary input channel.
        /// </summary>
        /// <param name="serviceIdentifier">The service identifier.</param>
        /// <returns></returns>
        public static string CreateNameForReactorCoreInputChannel(ServiceIdentifier serviceIdentifier)
        {
            return string.Format("Reactor.Core.{0}", serviceIdentifier.Name);
        }

        /// <summary>
        /// Gets typed version of the MessagingChannel name. That is, it's prefixed with 'queue://' to denote the MessagingChannel is a queue.
        /// </summary>
        /// <returns></returns>
        public override string GetTypedDestinationName()
        {
            var sb = new StringBuilder(2);
            sb.Append("queue://");
            sb.Append(DestinationName);

            return sb.ToString();
        }
    }
}
